﻿using System;
using NTasking.Internals;
using Void = NTasking.Internals.Void;

namespace NTasking
{

    public static class AsyncEx
    {

        private static ICallable<ACallDelegateT> GetCallable<ACallDelegateT>(Delegate entry)
            where ACallDelegateT : class // Should be MulticastDelegate but prevented by compiler
        {
            if (entry == null)
                throw Errors.AsynchronousCallIsNull();

            var callable = entry.Target as ICallable<ACallDelegateT>;

            if (callable == null)
                throw Errors.AsynchronousCallNotSynchronized(entry);

            var method = callable as SynchronizedMethod;

            // ReSharper disable PossibleNullReferenceException
            if (!method.IsEntry)
                // ReSharper restore PossibleNullReferenceException
                throw Errors.AsynchronousCallNotAnEntry(entry);

            return callable;
        }


        public static Future<RetT> Async<RetT>(this Func<RetT> entry)
        {
            var call = GetCallable<Func<MethodCall<RetT>>>(entry).Intercept();

            return SynchronizedObject.AynchronousCall(call);
        }


        public static Future Async(this Action entry)
        {
            var call = GetCallable<Func<MethodCall<Void>>>(entry).Intercept();

            return SynchronizedObject.AynchronousCall(call);
        }


        public static Future<RetT> Async<P1T, RetT>(this Func<P1T, RetT> entry, P1T p1)
        {
            var call = GetCallable<Func<P1T, MethodCall<RetT>>>(entry).Intercept(p1);

            return SynchronizedObject.AynchronousCall(call);
        }


        public static Future Async<P1T>(this Action<P1T> entry, P1T p1)
        {
            var call = GetCallable<Func<P1T, MethodCall<Void>>>(entry).Intercept(p1);

            return SynchronizedObject.AynchronousCall(call);
        }


        public static Future<RetT> Async<P1T, P2T, RetT>(this Func<P1T, P2T, RetT> entry, P1T p1, P2T p2)
        {
            var call = GetCallable<Func<P1T, P2T, MethodCall<RetT>>>(entry).Intercept(p1, p2);

            return SynchronizedObject.AynchronousCall(call);
        }


        public static Future Async<P1T, P2T>(this Action<P1T, P2T> entry, P1T p1, P2T p2)
        {
            var call = GetCallable<Func<P1T, P2T, MethodCall<Void>>>(entry).Intercept(p1, p2);

            return SynchronizedObject.AynchronousCall(call);
        }


        public static Future<RetT> Async<P1T, P2T, P3T, RetT>(this Func<P1T, P2T, P3T, RetT> entry, P1T p1, P2T p2, P3T p3)
        {
            var call = GetCallable<Func<P1T, P2T, P3T, MethodCall<RetT>>>(entry).Intercept(p1, p2, p3);

            return SynchronizedObject.AynchronousCall(call);
        }


        public static Future Async<P1T, P2T, P3T>(this Action<P1T, P2T, P3T> entry, P1T p1, P2T p2, P3T p3)
        {
            var call = GetCallable<Func<P1T, P2T, P3T, MethodCall<Void>>>(entry).Intercept(p1, p2, p3);

            return SynchronizedObject.AynchronousCall(call);
        }


        public static Future<RetT> Async<P1T, P2T, P3T, P4T, RetT>(this Func<P1T, P2T, P3T, P4T, RetT> entry, P1T p1, P2T p2, P3T p3, P4T p4)
        {
            var call = GetCallable<Func<P1T, P2T, P3T, P4T, MethodCall<RetT>>>(entry).Intercept(p1, p2, p3, p4);

            return SynchronizedObject.AynchronousCall(call);
        }


        public static Future Async<P1T, P2T, P3T, P4T>(this Action<P1T, P2T, P3T, P4T> entry, P1T p1, P2T p2, P3T p3, P4T p4)
        {
            var call = GetCallable<Func<P1T, P2T, P3T, P4T, MethodCall<Void>>>(entry).Intercept(p1, p2, p3, p4);

            return SynchronizedObject.AynchronousCall(call);
        }

    }

}
